package com.example.mylibrarybase.yumengmeng16.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.yumengmeng16.bean.radiobuttonbean;

import java.util.ArrayList;

/**
 * Created by 35876 于萌萌
 * 创建日期: 9:53 . 2016年10月25日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class LinearLayoutViewGrup_16 extends LinearLayout {
    Context mContext;
    private View mInflate;
    private LinearLayout mActivity_main_16_listmeun_ll;

    public LinearLayoutViewGrup_16(Context context) {
        super(context);
        initView(context);
    }



    public LinearLayoutViewGrup_16(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public LinearLayoutViewGrup_16(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
   public interface  CallBack{
        void onItem(radiobuttonbean radiobuttonbean,int pos);
    }
    private void initView(Context context) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflate = inflater.inflate(R.layout.linearlayout_view_16, this);
        mActivity_main_16_listmeun_ll = (LinearLayout) mInflate.findViewById(R.id.activity_main_16_listmeun_ll);
    }
    public LinearLayoutViewGrup_16 setList(ArrayList<radiobuttonbean> list, final CallBack callBack ){
        RadioGroup radioGroup = new RadioGroup(mContext);
        RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewGroup.LayoutParams radioButtonlayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        radioGroup.setLayoutParams(layoutParams);
        for (int i = 0; i < list.size(); i++) {
            RadioButton radioButton = new RadioButton(mContext);
            radioButton.setLayoutParams(radioButtonlayoutParams);
            radioButton.setText(list.get(i).id);
            // radioButton.setPadding(5,5,5,5);
            radioButton.setTag(list.get(i));


            final int finalI = i;
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        RadioButton compoundButton1 = (RadioButton) compoundButton;
                        radiobuttonbean tag = (radiobuttonbean) compoundButton1.getTag();
                        callBack.onItem(tag, finalI);
                    }
                }
            });


            radioGroup.setOrientation(RadioGroup.VERTICAL);
            radioGroup.addView(radioButton);
            if (i==0){
                RadioButton radioButton1 = (RadioButton) radioGroup.getChildAt(0);
                radioButton1.setChecked(true);
            }
            LogUtils.i("添加设置了"+list.get(i).id);
        }
        mActivity_main_16_listmeun_ll.addView(radioGroup);


        return this;
    }
}
